import { Body, Controller, Del, Get, Inject, Param, Post } from '@midwayjs/core';
import { cityInf } from '../entity/city.entity';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BillCodeGenerator } from '../service/IdTool.entity';
@Controller('/city')
export class CityController {
  @InjectEntityModel(cityInf)
  repoCity: Repository<cityInf>;
  @Get('/query')
  async query() {
    const r = await this.repoCity.query('select * from city group by name');
    return r;
  }
  @Inject()
  billCodeInfo: BillCodeGenerator;
  @Post('/insert')
  async insert(@Body() dto: cityInf) {
    const r = dto;
    r.code = await this.billCodeInfo.nextCode('Ci');
    return await this.repoCity.save(r);
  }
  @Del('/remove/:id')
  async remove(@Param('id') id: string) {
    const r = await this.repoCity.findOneBy({ code: id });
    return await this.repoCity.remove(r);
  }
}
